package com.moi.checkonlistviewdemo;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ActionBarActivity {

    private ListView mListView;
    private List<Item> itemList = new ArrayList<Item>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initItem();
        mListView = (ListView) findViewById(R.id.list_view);
        final MyAdapter adapter = new MyAdapter(MainActivity.this, R.layout.list_item, itemList);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setPos(position);
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void initItem() {
        for (int i = 0; i < 100; i++) {
            Item one = new Item("item_" + (i * 4 + 1), R.mipmap.one);
            Item two = new Item("item_" + (i * 4 + 2), R.mipmap.two);
            Item three = new Item("item_" + (i * 4 + 3), R.mipmap.three);
            Item four = new Item("item_" + (i * 4 + 4), R.mipmap.four);

            itemList.add(one);
            itemList.add(two);
            itemList.add(three);
            itemList.add(four);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
